//
//  HaveHeaderTableViewController.m
//  shengxian_delivery
//
//  Created by pg on 2017/3/13.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "HaveHeaderTableViewController.h"

@interface HaveHeaderTableViewController ()
@property(nonatomic,strong)UIView *notDataView;
@property(nonatomic,assign)BOOL m_haveNotDataView;

@end

@implementation HaveHeaderTableViewController

- (instancetype)init
{
    self = [super initWithNibName:@"HaveHeaderTableViewController" bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = kF2F2F2;

    [self addHeaderView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
 
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
  
    
    // Do any additional setup after loading the view from its nib.
}

//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    
//}

-(void)configNotDataView:(UIImage *)image text:(NSString *)text
{
    if (!self.haveNotDataView) {
        UITapGestureRecognizer *zer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refreshData)];
        [self.notDataView addGestureRecognizer:zer];
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.userInteractionEnabled = YES;
        
        [self.tableView addSubview:self.notDataView];
        [self.notDataView addSubview:imageView];
        
        [self.notDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(self.tableView);
            make.center.equalTo(self.tableView);
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(imageView.image.size);
            make.centerX.equalTo(self.notDataView);
            make.centerY.equalTo(self.notDataView).with.offset(-70);
        }];
        
        if (text) {
            UILabel *label = [[UILabel alloc]init];
            label.text = text;
            label.textColor = k666666;
            
            [self.notDataView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.tableView);
                make.top.equalTo(imageView.mas_bottom).with.offset(20);
            }];
        }
        
    }
    self.m_haveNotDataView = YES;
}


-(UIView *)addHeaderView
{
    return self.headerView;
}

-(void)showOrHideDataView
{
    if (self.haveNotDataView) {
        if (self.dataArray.count == 0) {
            self.notDataView.hidden = NO;
        }else{
            self.notDataView.hidden = YES;
        }
    }
}

-(void)addMj_header
{
    [self.tableView setMj_header:[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)]];
}

-(void)addMj_footer
{
    [self.tableView setMj_footer:[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)]];
}

-(void)refreshData
{
    self.listApi.page = 1;
    [self loadData];
}

-(void)loadMore
{
    self.listApi.page ++ ;
    [self loadData];
}

-(void)endRefreshing
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(NSMutableArray *)dataArray
{
    if (_dataArray==nil) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(BOOL)haveNotDataView
{
    return self.m_haveNotDataView;
}

-(UIView *)notDataView
{
    if (!_notDataView) {
        _notDataView = [UIView new];
        _notDataView.userInteractionEnabled = YES;
        _notDataView.backgroundColor = [UIColor whiteColor];
    }
    return _notDataView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
